package com.inspur.demo.controller;

import com.inspur.demo.common.CommResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.eureka.EurekaDiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 *    
 * 消费者控制层
 *
 * @desc:
 * @author: li.peng02
 * @date: 2019/2/28
 * @version: v1.0 
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private RestTemplate restTemplate;

    // 以下两个客户端类,用哪个都可以,DiscoveryClient抽象程度更高,是一个接口,EurekaDiscoveryClient只是他的抽象类
    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private EurekaDiscoveryClient eurekaDiscoveryClient;

    @RequestMapping("/test")
    public ResponseEntity<CommResultMsg> test() {
        return restTemplate.getForEntity("http://spring-cloud-producer/producer/test?name=123", CommResultMsg.class);
    }


    // eureka使用展示
    @GetMapping("/eureka/test")
    public String eureka() {
        System.out.println("eureka使用展示:/eureka/get---start");

        // ----------------- 以下的代码使用DiscoveryClient-------------------
        // 查询所有注册到Eureka上的服务:
        System.out.println("[discoveryClient]-查询所有注册到Eureka上的服务:");
        for (String string : discoveryClient.getServices()) {
            System.out.println(string);
        }

        // 查询某一个provider的所有Service实例
        System.out.println("[discoveryClient]-查询某一个provider的所有Service实例");
        List<ServiceInstance> instances = discoveryClient.getInstances("spring-cloud-producer");
        for (ServiceInstance instance : instances) {
            System.out.println("host:" + instance.getHost() + ",port:" + instance.getPort() + ",serviceId=" + instance.getServiceId() + ",uri=" + instance.getUri());
            System.out.println("metadata=" + instance.getMetadata());
        }

        // ----------------- 以下的代码使用EurekaDiscoveryClient-------------------

        // 查询所有注册到Eureka上的服务:
        System.out.println("[eurekaDiscoveryClient]-查询所有注册到Eureka上的服务:");
        for (String string : eurekaDiscoveryClient.getServices()) {
            System.out.println(string);
        }

        // 查询某一个provider的所有Service实例
        System.out.println("[eurekaDiscoveryClient]-查询某一个provider的所有Service实例");
        instances = eurekaDiscoveryClient.getInstances("spring-cloud-producer");
        for (ServiceInstance instance : instances) {
            System.out.println("host:" + instance.getHost() + ",port:" + instance.getPort() + ",serviceId=" + instance.getServiceId() + ",uri=" + instance.getUri());
            System.out.println("metadata=" + instance.getMetadata());
        }

        System.out.println("eureka使用展示:/eureka/get---end");
        return "eureka's demo, please to see console output";
    }
}
